package com.example.xyr.myapplication.utils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.xyr.myapplication.R;

/**
 * Created by ylcf on 2018/7/26.
 */

public class PoputWindowUtils {

    // 声明PopupWindow
    private PopupWindow popupWindow;
    // 声明PopupWindow对应的视图
    private View popupView;

    // 声明平移动画
    private TranslateAnimation animation;

    private static PoputWindowUtils poputWindowUtils;

    public enum Type {
        CAMERA, GALLERY, default1, default2
    }


    public static PoputWindowUtils getInstance() {
        if (poputWindowUtils == null) {
            poputWindowUtils = new PoputWindowUtils();
        }
        return poputWindowUtils;
    }

    private PoputWindowUtils() {

    }

    public void showPopmenu(final Activity activity, View popupView, final OnMenuItemClickListener listener) {
        lightoff(activity);
        if (popupWindow == null) {
            popupView = View.inflate(activity, R.layout.poput_win_layout, null);
            // 参数2,3：指明popupwindow的宽度和高度
            popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT);

            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    lighton(activity);
                }
            });

            // 设置背景图片， 必须设置，不然动画没作用
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);

            // 设置点击popupwindow外屏幕其它地方消失
            popupWindow.setOutsideTouchable(true);

            // 平移动画相对于手机屏幕的底部开始，X轴不变，Y轴从1变0
            animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                    Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
            animation.setInterpolator(new AccelerateInterpolator());
            animation.setDuration(200);

            popupView.findViewById(R.id.tvTakePhoto).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 打开系统拍照程
                    listener.onMenuItemClick(Type.CAMERA);
                }
            });
            popupView.findViewById(R.id.tvSelectPhoto).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 打开系统图库选择图片
                    listener.onMenuItemClick(Type.GALLERY);
                }
            });
        }

        // 在点击之后设置popupwindow的销毁
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
            lighton(activity);
        }

        // 设置popupWindow的显示位置，此处是在手机屏幕底部且水平居中的位置
        popupWindow.showAtLocation(popupView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//        popupView.startAnimation(animation);
    }

    //弹出的文字 自己定义
    public void showDefaultPopmenu(final Activity activity, View popupView, final OnMenuItemClickListener listener, String... strs) {
        lightoff(activity);
        if (popupWindow == null) {
            popupView = View.inflate(activity, R.layout.poput_win_layout, null);
            // 参数2,3：指明popupwindow的宽度和高度
            popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT);

            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    lighton(activity);
                }
            });

            // 设置背景图片， 必须设置，不然动画没作用
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);

            // 设置点击popupwindow外屏幕其它地方消失
            popupWindow.setOutsideTouchable(true);

            // 平移动画相对于手机屏幕的底部开始，X轴不变，Y轴从1变0
            animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                    Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
            animation.setInterpolator(new AccelerateInterpolator());
            animation.setDuration(200);

            int i = 0;
            TextView text1 = popupView.findViewById(R.id.tvTakePhoto);
            TextView text2 = popupView.findViewById(R.id.tvSelectPhoto);
            for (String str : strs) {
                if (i == 0) {
                    text1.setText(str);
                    text1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 回调1
                            listener.onMenuItemClick(Type.default1);
                        }
                    });
                    text2.setVisibility(View.GONE);
                } else if (i > 0 && i < 2) {
                    text2.setVisibility(View.VISIBLE);
                    text2.setText(str);
                    text2.findViewById(R.id.tvSelectPhoto).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 回调2
                            listener.onMenuItemClick(Type.default2);
                        }
                    });
                }
                i++;
            }

        }

        // 在点击之后设置popupwindow的销毁
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
            lighton(activity);
        }

        // 设置popupWindow的显示位置，此处是在手机屏幕底部且水平居中的位置
        popupWindow.showAtLocation(popupView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//        popupView.startAnimation(animation);
    }

    public void dismissPopmenu() {
        if (popupWindow != null) {
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
        }
    }

    public boolean popWinIsShow() {
        if (popupWindow == null) {
            return false;
        } else {
            return popupWindow.isShowing();
        }
    }

    public void destroy() {
        if (popupWindow != null) {
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
            popupWindow = null;
        }

        if (animation != null) {
            animation = null;
        }
    }

    /**
     * 设置手机屏幕亮度变暗
     */
    private void lightoff(Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.3f;
        activity.getWindow().setAttributes(lp);
    }

    /**
     * 设置手机屏幕亮度显示正常
     */
    private void lighton(Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 1f;
        activity.getWindow().setAttributes(lp);
    }

    public interface OnMenuItemClickListener {
        void onMenuItemClick(Type type);
    }
}
